﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace MiniFox.Data.Mapping
{
    /// <summary>
    /// 列映射类型
    /// </summary>
    public class ColumnMapping : IColumnMapping
    {
        string _dataSetColumn;
        string _sourceColumn;
        bool _primaryKey;
        bool _autoIncrement;
        bool _unique;
        bool _nullable;
        bool _readonly;
        bool _sortable;
        bool _sortMode;
        int _size;
        string _displayCaption;
        TypeCode _typeCode;
        ITableMapping _tableMapping;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sourceColumn">数据库(源)列名</param>
        /// <param name="dataSetColumn">数据集(对象)列名</param>
        /// <param name="tableMapping">隶属表映射对象</param>
        internal ColumnMapping(string sourceColumn, string dataSetColumn, ITableMapping tableMapping)
        {
            _dataSetColumn = dataSetColumn;
            _sourceColumn = string.IsNullOrEmpty(sourceColumn) ? dataSetColumn : sourceColumn;
            _tableMapping = tableMapping;
            _primaryKey = false;
            _unique = false;
            _nullable = true;
            _readonly = false;
            _sortable = false;
            _autoIncrement = false;
            _typeCode = TypeCode.String;
        }
        /// <summary>
        /// 是否为主键列
        /// </summary>
        public bool PrimaryKey
        {
            get { return _primaryKey; }
            set { _primaryKey = value; }
        }
        /// <summary>
        /// 是否为自增列
        /// </summary>
        public bool AutoIncrement
        {
            get { return _autoIncrement; }
            set { _autoIncrement = value; }
        }
        /// <summary>
        /// 是否为唯一键列
        /// </summary>
        public bool Unique
        {
            get { return this.PrimaryKey ? true : _unique; }
            set { _unique = this.PrimaryKey ? true : value; }
        }
        /// <summary>
        /// 是否可空
        /// </summary>
        public bool Nullable
        {
            get { return this.PrimaryKey ? false : _nullable; }
            set { _nullable = this.PrimaryKey ? false : value; }
        }
        /// <summary>
        /// 是否只读
        /// </summary>
        public bool ReadOnly
        {
            get { return _readonly; }
            set { _readonly = value; }
        }
        /// <summary>
        /// 字段长度
        /// </summary>
        public int Size
        {
            get { return _size; }
            set { _size = value; }
        }
        
        /// <summary>
        /// 是否可排序
        /// </summary>
        public bool Sortable
        {
            get { return _sortable; }
            set { _sortable = value; }
        }
        /// <summary>
        /// 排序方式
        /// </summary>
        public bool SortMode
        {
            get { return _sortMode; }
            set { _sortMode = value; }
        }
        
        /// <summary>
        /// 显示名
        /// </summary>
        public string DisplayCaption
        {
            get { return _displayCaption; }
            set { _displayCaption = value; }
        }
        /// <summary>
        /// 列数据类型
        /// </summary>
        public TypeCode Type
        {
            get { return _typeCode; }
            set { _typeCode = value; }
        }
        /// <summary>
        /// 隶属表映射对象
        /// </summary>
        public ITableMapping TableMapping
        {
            get { return _tableMapping; }
        }

        #region IColumnMapping 成员
        /// <summary>
        /// 数据集(对象)列名
        /// </summary>
        public string DataSetColumn
        {
            get { return _dataSetColumn; }
            set { _dataSetColumn = value; }
        }
        /// <summary>
        /// 数据库(源)列名
        /// </summary>
        public string SourceColumn
        {
            get { return _sourceColumn; }
            set { _sourceColumn = value; }
        }
        /// <summary>
        /// 
        /// </summary>
        public int Ordinal { get; set; }

        #endregion
    }
}
